
/*
 * 文件名：TestCatServiceImpl.java.
 * 描述：
 * 创建人: 龚洪亮
 * 创建时间：2017年8月8日
 */

package com.fiberhome.platform.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.fiberhome.platform.common.EUTreeNode;
import com.fiberhome.platform.common.PageResult;
import com.fiberhome.platform.mapper.RelationMapMapper;
import com.fiberhome.platform.mapper.TestCaseMapper;
import com.fiberhome.platform.mapper.TestCategoryMapper;
import com.fiberhome.platform.pojo.RelationMapExample;
import com.fiberhome.platform.pojo.TestCaseExample;
import com.fiberhome.platform.pojo.TestCategory;
import com.fiberhome.platform.pojo.TestCategoryExample;
import com.fiberhome.platform.pojo.TestCategoryExample.Criteria;



/**
 * @Description 
 * @author 龚洪亮
 * @version 2017年8月8日
 */
@Service
public class TestCatServiceImpl implements TestCatService {
	@Autowired
	private TestCategoryMapper testCatMapper;
	@Autowired
	private TestCaseMapper testCaseMapper;
	@Autowired
	private RelationMapMapper relationMapMapper;
	@Override
	public List<EUTreeNode> getTestCat(Integer parentId) {
		// TODO Auto-generated method stub
		TestCategoryExample example = new TestCategoryExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentIdEqualTo(parentId);
		List<TestCategory> list = testCatMapper.selectByExample(example);
		List<EUTreeNode> result = new ArrayList<EUTreeNode>();
		
		for (TestCategory testCat : list) {
			EUTreeNode treeNode = new EUTreeNode();
			treeNode.setId(testCat.getId());
			treeNode.setText(testCat.getName());
			treeNode.setState(testCat.getIsParent()==1?"closed":"open");
			treeNode.setType(testCat.getType());
			treeNode.setCaseNo(testCat.getCaseNo());
			treeNode.setIsParent(testCat.getIsParent());
			result.add(treeNode);
		}
		return result;
	}

	@Override
	@Transactional
	public PageResult createTest(TestCategory test) {
		TestCategory testCat = new TestCategory();
		testCat.setParentId(test.getParentId());
		testCat.setName(test.getName());
		testCat.setSortOrder(1);
		testCat.setIsParent(0);
		testCat.setCreated(new Date());
		testCat.setUpdated(new Date());
		testCat.setStatus(1);
		testCat.setType(1);
		TestCategory parentCat = testCatMapper.selectByPrimaryKey(test.getParentId());
		if (!(parentCat.getIsParent()==1)) {
			parentCat.setIsParent(1);
			testCatMapper.updateByPrimaryKey(parentCat);
		}
		testCatMapper.insert(testCat);
		
		//System.out.println(insertStat);
		return PageResult.ok(testCat);
	}

	@Override
	@Transactional
	public PageResult updateTest(TestCategory testCat) {
//		if (testCat.getCaseNo()!=null) {
//			//获取叶节点上的所有父节点，目的是为了定位最后一级的位置，从而更新最后一级的text
//			TestCategoryExample example = new TestCategoryExample();
//			example.createCriteria().andCaseNoEqualTo(testCat.getCaseNo());
//			List<TestCategory> testCats = testCatMapper.selectByExample(example);
//			List<TestCategory> parentList = testCatMapper.selectAllParent(testCats.get(0).getId());
//			
//			//根据caseNo获取详情
//			TestCaseExample example_test = new TestCaseExample();
//			example_test.createCriteria().andCaseNoEqualTo(testCat.getCaseNo());
//			List<TestCase> test_detail = testCaseMapper.selectByExample(example_test);
//			TestCase test = test_detail.get(0);
//			int size = parentList.size();
//			
//			if (size==0) {
//				
//			} else if (size==1) {
//				test.setTestClass1(testCat.getName());
//			} else if (size==2) {
//				test.setTestClass2(testCat.getName());
//			} else if (size == 3) {
//				test.setTestClass3(testCat.getName());
//			} else if (size==4) {
//				test.setTestClass4(testCat.getName());
//			}
//			testCaseMapper.updateByPrimaryKeySelective(test);
//		}
		testCat.setUpdated(new Date());
		int updateStat = testCatMapper.updateByPrimaryKeySelective(testCat);
		//System.out.println(updateStat);
		if (updateStat > 0) {
			return PageResult.ok(testCat);
		}
		return PageResult.ok();
	}

	@Override
	@Transactional
	public PageResult deleteTest(Integer id, String caseNo) {
		if (caseNo==null) {
			//删除分类节点
			TestCategory selectCon = testCatMapper.selectByPrimaryKey(id);
			Integer parentId = selectCon.getParentId();
			TestCategory parentCon = testCatMapper.selectByPrimaryKey(parentId);
			deleteNowAndChildNode(id);
			if (!(hasChildNode(parentId))) {
				parentCon.setIsParent(0);
				testCatMapper.updateByPrimaryKey(parentCon);
			}
			return PageResult.ok(selectCon);
		} else {
			//删除根节点对应的测试用例详情记录
			TestCaseExample example = new TestCaseExample();
			example.createCriteria().andCaseNoEqualTo(caseNo);
			testCaseMapper.deleteByExample(example);
			
			
			//删除包含此条记录的映射表中的记录
			RelationMapExample map_exmaple = new RelationMapExample();
			map_exmaple.createCriteria().andCaseNoEqualTo(caseNo);
			relationMapMapper.deleteByExample(map_exmaple);
			
			//删除分类节点
			TestCategory selectCon = testCatMapper.selectByPrimaryKey(id);
			Integer parentId = selectCon.getParentId();
			TestCategory parentCon = testCatMapper.selectByPrimaryKey(parentId);
			deleteNowAndChildNode(id);
			if (!(hasChildNode(parentId))) {
				parentCon.setIsParent(0);
				testCatMapper.updateByPrimaryKey(parentCon);
			}
			return PageResult.ok(selectCon);
		}
	}
	
	@Transactional
	private void deleteNowAndChildNode(Integer id) {
		TestCategory selectCon = testCatMapper.selectByPrimaryKey(id);
		//如果当前节点是父节点，则选出所有子节点
		if (selectCon.getIsParent()==1) {
			TestCategoryExample example = new TestCategoryExample();
			Criteria criteria = example.createCriteria();
			criteria.andParentIdEqualTo(id);
			List<TestCategory> childList = testCatMapper.selectByExample(example);
			for (TestCategory child : childList) {
				deleteNowAndChildNode(child.getId());
			}
			//子节点删除完成之后，本节点也要删除
			testCatMapper.deleteByPrimaryKey(id);
			
		} else {
			testCatMapper.deleteByPrimaryKey(id);
		}
	}
	
	private boolean hasChildNode(Integer id) {
		TestCategoryExample example = new TestCategoryExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentIdEqualTo(id);
		List<TestCategory> childList = testCatMapper.selectByExample(example);
		if (childList.size() == 0) {
			return false;
		}
		return true;
	}

	@Override
	public TestCategory getById(Integer id) {
		TestCategory test = testCatMapper.selectByPrimaryKey(id);
		return test;
	}

}
